package checkLocation;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xmlParser.xmlReader;

public class location {
	
	public static int checkLocationWiki(String place){
		place=firstCapital(place).replace(' ', '+');
		
		String url="http://wikitravel.org/wiki/en/api.php?action=query&format=xml&titles=";
		boolean flag=false;
		url=url+place;	
		try{
			
			Document doc=xmlReader.createDocUrl(url);
			NodeList n1=doc.getElementsByTagName("page");
	
			Element e1=(Element)n1.item(0);
			
			if(Integer.parseInt(e1.getAttribute("pageid"))>0)
				flag=true;
				
		}catch(Exception e){
			
		}
		
		if(flag)
			return 1;
		else
			return 0;
	}
	
	
	public static boolean checkLocationGWS(String place){
		
		
		
		String place1=firstCapital(place).replace(' ', '+');
		
		String url="http://gws.maps.yahoo.com/FindLocation?type=single-line+find&appid=search&locale=us&flags=&gflags=q&q=";
		List<String> tokenArr=new ArrayList<String>();
		boolean flag=false;
		url=url+place1;
		
		StringTokenizer tokens=new StringTokenizer(place);
		while(tokens.hasMoreTokens()){
			tokenArr.add(tokens.nextToken().toLowerCase());
		}
		
		
	
		try{
			
			Document doc=xmlReader.createDocUrl(url);
			
	
			
		if(doc.getElementsByTagName("Found").item(0).getTextContent().toString().compareTo("0")!=0)flag=true;
		
		
		/*if(tokenArr.contains(doc.getElementsByTagName("country").item(0).getTextContent().toString().toLowerCase()))flag=true;
		if(tokenArr.contains(doc.getElementsByTagName("county").item(0).getTextContent().toString().toLowerCase()))flag=true;
		if(tokenArr.contains(doc.getElementsByTagName("state").item(0).getTextContent().toString().toLowerCase()))flag=true;
		if(tokenArr.contains(doc.getElementsByTagName("city").item(0).getTextContent().toString().toLowerCase()))flag=true;
		
			*/
			
			
				
		}catch(Exception e){
			System.err.println("error");
			
		}
		
	 
		return flag;
	}
	
	
public static boolean checkLocationQpUs(String place){
		
		
		
		String place1=firstCapital(place).replace(' ', '+');
		
		String url="http://qp-us-west.search.yahoo.com:4080/qp.php?src=image&site=yahoo&testid=I147&uq=";
		List<String> tokenArr=new ArrayList<String>();
		boolean flag=false;
		url=url+place1;
		System.out.println(url);
		
		StringTokenizer tokens=new StringTokenizer(place);
		while(tokens.hasMoreTokens()){
			tokenArr.add(tokens.nextToken().toLowerCase());
		}
		
		
	
		try{
			
			Document doc=xmlReader.createDocUrl(url);
			
	
			
			if(Float.parseFloat(doc.getElementsByTagName("conf").item(0).getTextContent().toString())>50)flag=true;
		
				
		}catch(Exception e){
			System.err.println("error");
			
		}
		
	 
		return flag;
	}
	
	
	/*
	 * new york==>> New York
	 * NEW YORK ==>>New York
	 */
	public static String firstCapital(String place){
		
		int i=0;
		place=place.toLowerCase();
		
		StringBuffer abc=new StringBuffer();
		for(i=0;i<place.length();i++){
			if(i==0){
				int temp=(int)place.charAt(i)-32;
				abc.append((char)temp);
			}
			else if(place.charAt(i)==32){
				abc.append(place.charAt(i));
				i=i+1;
				abc.append((char)(place.charAt(i)-32));				
			}
			else
				abc.append(place.charAt(i));
				
		}
		return abc.toString();
	}
	
	/*
	 * Given a string iterates the string with different length sizes and then finally predicts the location in the string..
	 * 
	 * eg: large car in new york city
	 * 
	 */
	
	public static String contains(String label){
		ArrayList<String> tokens=new ArrayList<String>();
		int i,j,k;
		StringTokenizer abc=new StringTokenizer(label);		
		while(abc.hasMoreTokens()){
			tokens.add(abc.nextToken());
		}
		for(i=tokens.size();i>=1; i--){
			for(j=0;j<tokens.size();j++){
				String temp="";
				for(k=j;k<j+i && k<tokens.size();k++){
				
					temp=temp+tokens.get(k)+" ";
				}
				temp=temp.substring(0, temp.length()-1);
				if(checkLocationWiki(temp)==1)
				{	
					return temp;
				}				
			}
		}
		return null;
	}
	
	
	
	public static void main(String argsp[]){
		
		System.out.println(contains("large car in halsuru"));
		
	}
	
	
public static String labelPredictionQueries(String place,String Query){
		
		
		place=place+" "+Query;
		String query=place.replace(" ", "+OR+");
		
		String url="http://riddentable-dr.bagmane.corp.yahoo.com:19010/search?query="+query+"&type=adv";
		List<String> tokenArr=new ArrayList<String>();
		boolean flag=false;
		System.out.println(url);
		
		int hit=0;
		String thumbnails=null;
		
		
	
		try{
			
			Document doc=xmlReader.createDocUrl(url);
			NodeList nList = doc.getElementsByTagName("hit");
			for (int temp = 0; temp < nList.getLength(); temp++) {
				
				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					if(hit==1)break;
					Element eElement = (Element) nNode;
					thumbnails=xmlReader.getAtributeValue("text", eElement, "field", "name");
					hit++;
					System.out.println(thumbnails);
					
					
				}
			}
	
			
			//if(Float.parseFloat(doc.getElementsByTagName("conf").item(0).getTextContent().toString())>50)flag=true;
		
				
		}catch(Exception e){
			System.err.println("error");
			
		}
		
	 
		return thumbnails;
	}

}
